package ping.mp3player;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Queue;

import ping.lrc.LrcProcessor;
import ping.model.Mp3Info;
import ping.mp3player.service.PlayerService;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class PlayerActivity extends Activity {
	ImageButton playBtn = null;
	ImageButton pauseBtn = null;
	ImageButton stopBtn = null;
	MediaPlayer mediaPlayer = null;
	TextView lrcTextView = null;
	private Mp3Info mp3Info = null;
	private ArrayList<Queue> queues = null;
	private long begin = 0;
	private long nextTimeMill = 0;
	private long currentTimeMill = 0;
	private long pauseTimerTills = 0;
	private boolean isPlaying = false;
	private String message = null;
	private UpdateTimeCallback updateTimeCallback = null;
	private Handler handler =null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player);
		Intent intent = getIntent();
	    mp3Info  = (Mp3Info) intent.getSerializableExtra("mp3info");
		System.out.println(mp3Info);
		
		playBtn = (ImageButton) findViewById(R.id.play);
		pauseBtn = (ImageButton) findViewById(R.id.pause);
		stopBtn = (ImageButton) findViewById(R.id.stop );
		lrcTextView = (TextView) findViewById(R.id.lrcText);
		
		playBtn.setOnClickListener(new PlayButtonListener());
		pauseBtn.setOnClickListener(new PauseButtonListener());
		stopBtn.setOnClickListener(new StopButtonListener());
	}
	/**
	 * 根据歌词文件内容读取歌词文件内的信息
	 * @param lrcName
	 */
	private void prepareLrc(String lrcName){
		
		try{
			String path = Environment.getExternalStorageDirectory().getAbsolutePath() +
			File.separator + "mp3" + File.separator + mp3Info.getLrcName();
			InputStream inputStream = new FileInputStream(path);
			LrcProcessor lrcProc = new LrcProcessor();
			queues  = lrcProc.process(inputStream);
			//创建一个updatecallback对象 
			handler = new Handler();
			updateTimeCallback = new UpdateTimeCallback(queues);
			begin = 0;
			currentTimeMill = 0;
			nextTimeMill = 0;
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	class PlayButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			//启动Service并把Mp3内容传递给service
			Intent intent= new Intent();			
			intent.setClass(PlayerActivity.this, PlayerService.class);
			System.out.println("mp3 lrc-->" + mp3Info.getLrcName());
			intent.putExtra("mp3info", mp3Info);
			intent.putExtra("MSG", AppConstant.PlayerMsg.PLAY_MSG);
			//读取LRC文件
			prepareLrc(mp3Info.getLrcName());
			startService(intent);
			//开始计时
			begin = System.currentTimeMillis();
			//执行回调函数
			//handler.postDelayed(updateTimeCallback, 5);
			handler.post(updateTimeCallback);
			isPlaying = true;
		}		
	}
	class PauseButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			//通知service暂停
			Intent intent = new Intent();
			intent.setClass(PlayerActivity.this, PlayerService.class);
			intent.putExtra("MSG", AppConstant.PlayerMsg.PAUSE);
			startService(intent);
			if(isPlaying){
				handler.removeCallbacks(updateTimeCallback);
				pauseTimerTills = System.currentTimeMillis();
			} else {
				handler.postDelayed(updateTimeCallback, 5);
				begin = System.currentTimeMillis() - pauseTimerTills;
			}
			isPlaying = isPlaying ? false : true;
		}		
	}
	class StopButtonListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(PlayerActivity.this, PlayerService.class);
			intent.putExtra("MSG", AppConstant.PlayerMsg.STOP);
			startService(intent);
			//从handler当中移除回调函数
			handler.removeCallbacks(updateTimeCallback);
		}		
	}
	
	class UpdateTimeCallback implements Runnable{
		ArrayList<Queue> queues = null;
		Queue times = null;
		Queue messages = null;
		public UpdateTimeCallback(ArrayList<Queue> queues){
			this.queues = queues;
			times = queues.get(0);
			messages = queues.get(1);
			System.out.println("============////"+messages.size());
		}
		@Override
		public void run() {			
			long offset = System.currentTimeMillis() - begin ;
			System.out.println("offset:"+offset);
			if(currentTimeMill == 0){
				nextTimeMill = (Long) times.poll();
				message = (String) messages.poll();								
			}
			if(offset >= nextTimeMill){
				
				lrcTextView.setText(message);
				//lrcTextView.setText("fasdjfalsdfjasldkfasdklfjasdkfjsldkfjasdlfjsdlfj");
				message = (String) messages.poll();
				nextTimeMill = (Long) times.poll();
			}
			currentTimeMill = currentTimeMill + 10;
			handler.postDelayed(updateTimeCallback, 10);
		}
		
	}
	
}
